#include"boxgame.h"
#include<QString>
#include<QImage>
#include<QRect>
#include<memory.h>
BoxGame::BoxGame(){
    initMap();
}
void BoxGame::initMap(){
    int graph[8][8]={
        {1,1,1,1,1,1,1,1},
        {1,3,4,0,0,0,1,1},
        {1,0,1,1,1,0,1,1},
        {1,0,0,5,0,0,0,1},
        {1,0,1,0,1,0,1,1},
        {1,0,1,0,1,0,1,1},
        {1,0,0,0,4,3,0,1},
        {1,1,1,1,1,1,1,1}
    };
    memcpy(&map[0][0],&graph[0][0],sizeof(int)*64);
}
void BoxGame::drawMap(QPainter*pDraw){
       QString imgURL;
for (int i = 0;i < 8;i++) {
        for (int j = 0;j < 8;j++) {

            switch (map[i][j]) {
            case 0:
                imgURL="://map/道路.jpg";
                break;
            case 1:
                imgURL="://map/石墙.jpg";
                break;
            case 3:
                imgURL="://map/篮球.jpg";
                break;
            case 4:
                imgURL="://map/球网.jpg";
                break;
            case 5:
            case 8:
                imgURL="://map/坤坤.jpg";
                break;
            case 7:
                imgURL="://map/只因.jpg";
                break;
            }
            QImage img(imgURL);
            pDraw->drawImage(QRect(j*img.width(),i*img.height(),img.width(),img.height()),img);
        }
    }
}
int BoxGame::getRow(){
    for (int i = 0;i < 8;i++) {
        for (int j = 0;j < 8;j++) {
            if (map[i][j] == 5 || map[i][j] == 8) {
                return i;
            }
        }
    }
    return -1;
}
int BoxGame::getCols(){
    for (int i = 0;i < 8;i++) {
        for (int j = 0;j < 8;j++) {
            if (map[i][j] == 5 || map[i][j] == 8) {
                return j;
            }
        }
    }
    return -1;
}
void BoxGame::keyDown(QKeyEvent*event){
    int i = getRow();
    int j = getCols();
    switch (event->key()) {
    case Qt::Key_W:
    case Qt::Key_Up:
        if (map[i - 1][j] == 0 || map[i - 1][j] == 3) {
            map[i][j]-= 5;
            map[i - 1][j]+= 5;
        }
        if (map[i - 1][j] == 4 || map[i - 1][j] == 7) {
            if (map[i - 2][j] == 0 || map[i - 2][j] == 3) {
                map[i][j] -= 5;
                map[i - 1][j] += 1;
                map[i - 2][j] += 4;
            }
        }
        break;
    case Qt::Key_S:
    case Qt::Key_Down:
        if (map[i + 1][j] == 0 || map[i + 1][j] == 3) {
            map[i][j]-= 5;
            map[i + 1][j]+= 5;
        }
        if (map[i + 1][j] == 4 || map[i + 1][j] == 7) {
            if (map[i + 2][j] == 0 || map[i + 2][j] == 3) {
                map[i][j] -= 5;
                map[i + 1][j] += 1;
                map[i + 2][j] += 4;
            }
        }
        break;
    case Qt::Key_A:
    case Qt::Key_Left:
        if (map[i][j-1] == 0 || map[i][j-1] == 3) {
            map[i][j]-= 5;
            map[i][j-1]+= 5;
        }
        if (map[i][j-1] == 4 || map[i][j-1] == 7) {
            if (map[i][j-2] == 0 || map[i][j-2] == 3) {
                map[i][j] -= 5;
                map[i][j-1] += 1;
                map[i][j-2] += 4;
            }
        }
        break;
    case Qt::Key_D:
    case Qt::Key_Right:
        if (map[i][j +1] == 0 || map[i][j + 1] == 3) {
            map[i][j]-= 5;
            map[i][j + 1]+= 5;
        }
        if (map[i][j + 1] == 4 || map[i][j +1] == 7) {
            if (map[i][j + 2] == 0 || map[i][j + 2] == 3) {
                map[i][j] -= 5;
                map[i][j + 1] += 1;
                map[i][j + 2] += 4;
            }
        }
        break;
    }
}
bool BoxGame::gameover(){
    return false;
}

